import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import {PromptDto} from './dto/prompt.dto'
import { Prompt} from './entities/prompt.entity'
@Injectable()
export class PromptService {
  constructor(
    @InjectModel('Prompts') private readonly promptModel:Model<Prompt>
) {}
  async create(createPromptDto: PromptDto) {
    return await this.promptModel.insertMany(createPromptDto);
  }
  async findOne(title: string, id: string):Promise<PromptDto[]> {
    if(id)return await this.promptModel.find({_id:id});
    if(title)return await this.promptModel.find({title:{$regex:title}});
  }

  async update(id: string, updatePromptDto: PromptDto) {
    return await this.promptModel.updateOne({_id:id}, updatePromptDto);
  }

  async remove(id: string):Promise<any> {
    return await this.promptModel.deleteOne({_id:id});
  }

  async list(page: number, pageSize: number, userId: string):Promise<any> {
    const skip = (page - 1) * pageSize;
    const data = await this.promptModel.find({userId: userId}).sort({ time: -1 })
    const list = await this.promptModel.find({userId: userId}).sort({ time: -1 }).skip(skip).limit(pageSize)
    return {total: data.length, list: list}
  }
}
